/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie06.openvoip.client;

import com.rie06.openvoip.User;
import com.rie06.openvoip.server.ServerMessage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author clement
 */
public class Client_UpdateContactTemp implements Runnable {

    private static final Logger logger = Logger.getLogger(Client_UpdateContactTemp.class);
    private Client client;
    private Thread thread;

    public Client_UpdateContactTemp(Client client) {
        this.client = client;
        thread = new Thread(this);
        thread.setName("Client_UpdateContactTemp");
        thread.start();
    }

    public void run() {
        while (thread != null) {
            HashMap m = (HashMap) client.getContactListTemp();
            for (Iterator it = m.entrySet().iterator(); it.hasNext();) {
                Map.Entry<UUID, Contact> entry = (Map.Entry<UUID, Contact>) it.next();

                UUID uuid = entry.getKey();
                Contact contact = entry.getValue();
                if (!client.getServer().getUsersList().containsKey(uuid)) {
                    //connect
                    User user = client.connectTo(contact.getIpAddress(), contact.getPort(), "");
                    if (user == null) {
                        logger.log(Level.ERROR, "Cannot connect to user: " + contact.getName() + " - " + contact.getUuid());
                    } else {
                        //create Contact
                        Contact contactValidate = new Contact(user.getId(), user.getUserName(), user.getIpAddress(), user.getPort());
                        
                        //delete contact of temp list
                        client.getContactListTemp().remove(contact.getUuid());
                           logger.log(Level.DEBUG, "Delete user <" + user.getUserName() + "> in ContactListTemp: " + client.getContactListTemp().size());
                        
                        //save contact temp list
                        String configFile = System.getProperty("user.home") + "/.config/OpenVoip/Contacts.list.temp";
                        client.saveContactList(configFile, client.getContactListTemp());

                        //close connection
                        ServerMessage sm = new ServerMessage("close", null, client.getServer().getServerConfig().getId());
                        user.getServerTCPThread().sendMessage(sm);

                        //send user to IHM
                        client.getIhm().getAddContact().receiveAddContact(contactValidate);
                    }
                }
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                java.util.logging.Logger.getLogger(Client_UpdateContact.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
        }
    }
}
